<cfcomponent rest="true" restpath="/workweek">
	
	<!--- --->
	
	<cffunction name="getWorkweek" access="remote" httpmethod="GET" restpath="/getWorkweek">
		<cfargument name="userID" required="true" type="string" restArgSource="query" />
		<cfargument name="lastSync" required="false" default="" type="string" restArgSource="query" />

		<cfset var local = {} />
		
		<cfset local.user = $.request.setSession(arguments.userID) />
		
		<cfset local.response = {
			status=400,
			headers={},
			content=serializeJSON({error:"No Response"})
		} />
		
		<!--- when using modified_after, include recently deleted items --->
		<cfif arguments.lastSync neq "">
			<cfset arguments.modified_after = arguments.lastSync />
			<cfset arguments.active = "" />
		</cfif>
		
		<cftry>
			<cfif local.user.recordcount neq 1>
				<cfset local.response.status = 401 />
				<cfset local.response.content = "Authorization Failed" />
			<cfelse>
				<!--- get the events --->
				<cfset local.options = $.run.it('optionService.getOptions',{key="workweek",userID=arguments.userID}) />
				<cfset arguments.weekdays = local.options.workdays />
				<cfset arguments.scheduled = true />
				<cfset local.events = $.run.it('workweekService.getWorkweekEvents',arguments) />
				
				<cfset local.eventArray = [] />
				<cfloop query="local.events">
					<cfset local.event = {
						id=local.events.id,
						name=local.events.name,
						start_time=local.events.start_time,
						duration=local.events.duration,
						weekday=local.events.weekday,
						active=local.events.active
					} />
					<cfset arrayAppend(local.eventArray,local.event) />
				</cfloop>

				<cfset local.response.status = 200 />
				<cfset local.response.content = serializeJSON(local.eventArray) />
			</cfif>

			<cfset restSetResponse(local.response) />
			
			<cfcatch>
				<cfset application.helpers.log.add(
					name="REST_Error",
					method="getWorkweek",
					args=arguments,
					cfcatch=cfcatch
				) />
				<cfset local.response.status = "500" />
				<cfset local.response.content = "The server has encountered an error processing this request." />

				<!--- <cfsavecontent variable="local.dump">
					<cfdump var="#cfcatch#">
				</cfsavecontent>
				<cfset local.response.content = local.dump /> --->

				<cfset restSetResponse(local.response) />
			</cfcatch>
		</cftry>
		
	</cffunction>
	
	<!--- --->

</cfcomponent>